/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: graph.js 6 2012-03-10 12:51:49Z cc.hgfeng@gmail.com $
 */

/**
 * OTR.Graph
 * 提供图形处理接口工厂方法
 * 
 */
OTR.Graph = {
    /**
     * 支持接口类型
     * 
     * @type Array
     */
    _adapterTypes: ['canvas', 'vml', 'svg'],
        
    /**
     * 绘图接口对象工厂方法
     * 参数详见 @see OTR.Graph.Adapter
     * 
     * @param  object params
     * @return OTR.Graph.Adapter
     */
    factory: function(params, type) {
        if (typeof params !== 'object') {
            throw 'Invalid params for OTR.Graph Adapter to create';
            return null;
        }
        
        type = (undefined !== type && this._isSupported(type))
             ? type
             : this._adapterType();
        
        type = type.substring(0, 1).toUpperCase() + type.substring(1);

        return new OTR.Graph[type](params);
    },
    
    /**
     * 获取当前客户端可用的最佳接口类型
     * 
     * @private
     * @return string
     */
    _adapterType: function() {
        var canvas = document.createElement('canvas');
        // :-) 优先采用HTML5 Canvas
        if (undefined !== canvas.getContext) {
            return 'canvas';

        // :-( 不支持 canvas的IE浏览器使用vml
        } else if (OTR.Client.browser(OTR.Client.MSIE)) {
            return 'vml';
        }
        
        // :-|
        return 'svg';
    },
    
    /**
     * 判断当前接口类型是否支持
     */
    _isSupported: function(type) {
        if (typeof type !== 'string') {
            return ;
        }
        type = type.toLowerCase();
        
        var sp = this._adapterTypes, c = sp.length, i = 0;
        for ( ; i < c; i++) {
            if (sp[i] == type) {
                return true;
            }
        }
        
        return false;
    }
};